1
|
|
|
// Type definitions for byte-data 19.0 |
2
|
|
|
// Project: https://github.com/rochars/byte-data |
3
|
|
|
// Definitions by: Rafael da Silva Rocha <https://github.com/rochars> |
4
|
|
|
// Definitions: https://github.com/rochars/byte-data |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Read a string of UTF-8 characters from a byte buffer. |
8
|
|
|
* @param {!(Uint8Array|Array<number>)} buffer A byte buffer. |
9
|
|
|
* @param {number} [index=0] The buffer index to start reading. |
10
|
|
|
* @param {number} [end=buffer.length] The index to stop reading, non inclusive. |
11
|
|
|
* @return {string} |
12
|
|
|
*/ |
13
|
|
|
export function unpackString( |
14
|
|
|
buffer: Uint8Array|number[], |
15
|
|
|
index?: number, |
16
|
|
|
end?: number): string; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Write a string of UTF-8 characters as a byte buffer. |
20
|
|
|
* @param {string} str The string to pack. |
21
|
|
|
* @return {!Array<number>} The UTF-8 string bytes. |
22
|
|
|
* @throws {TypeError} If 'str' is not a string. |
23
|
|
|
*/ |
24
|
|
|
export function packString( |
25
|
|
|
str: string): number[]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Write a string of UTF-8 characters to a byte buffer. |
29
|
|
|
* @param {string} str The string to pack. |
30
|
|
|
* @param {!(Uint8Array|Array<number>)} buffer The output buffer. |
31
|
|
|
* @param {number} [index=0] The buffer index to start writing. |
32
|
|
|
* @return {number} The next index to write in the buffer. |
33
|
|
|
* @throws {TypeError} If 'str' is not a string. |
34
|
|
|
*/ |
35
|
|
|
export function packStringTo( |
36
|
|
|
str: string, |
37
|
|
|
buffer: Uint8Array|number[], |
38
|
|
|
index?: number): number; |
39
|
|
|
|
40
|
|
|
// Numbers |
41
|
|
|
/** |
42
|
|
|
* Pack a array of numbers to a byte buffer. |
43
|
|
|
* All other packing functions are interfaces to this function. |
44
|
|
|
* @param {!(Array<number>|TypedArray)} values The values to pack. |
45
|
|
|
* @param {!{bits:number, fp: (boolean|undefined), signed: (boolean|undefined), be: (boolean|undefined)}} theType The type definition. |
46
|
|
|
* @param {!(Uint8Array|Array<number>)} buffer The buffer to write on. |
47
|
|
|
* @param {number} [index=0] The buffer index to start writing. |
48
|
|
|
* @param {boolean} [clamp=false] True to clamp ints on overflow. |
49
|
|
|
* @return {number} The next index to write. |
50
|
|
|
* @throws {Error} If the type definition is not valid. |
51
|
|
|
* @throws {RangeError} On overflow if clamp is set to false. |
52
|
|
|
* @throws {TypeError} If 'values' is not a array of numbers. |
53
|
|
|
* @throws {TypeError} If 'values' is not a array of ints and type is int. |
54
|
|
|
*/ |
55
|
|
|
export function packArrayTo( |
56
|
|
|
values: number[]|ArrayBufferView|ArrayLike<number>, |
57
|
|
|
theType: object, |
58
|
|
|
buffer: Uint8Array|number[], |
59
|
|
|
index?: number, |
60
|
|
|
clamp?: boolean): number; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Unpack a array of numbers from a byte buffer to a array or a typed array. |
64
|
|
|
* All other unpacking functions are interfaces to this function. |
65
|
|
|
* @param {!(Uint8Array|Array<number>)} buffer The byte buffer. |
66
|
|
|
* @param {!{bits:number, fp: (boolean|undefined), signed: (boolean|undefined), be: (boolean|undefined)}} theType The type definition. |
67
|
|
|
* @param {!(TypedArray|Array<number>)} output The output array or typed array. |
68
|
|
|
* @param {number} [start=0] The buffer index to start reading. |
69
|
|
|
* @param {number} [end=buffer.length] The buffer index to stop reading. |
70
|
|
|
* @param {boolean} [safe=false] If set to false, extra bytes in the end of |
71
|
|
|
* the input array are ignored and input buffers with insufficient bytes will |
72
|
|
|
* write nothing to the output array. If safe is set to true the function |
73
|
|
|
* will throw a 'Bad buffer length' error on the aforementioned cases. |
74
|
|
|
* @throws {Error} If the type definition is not valid. |
75
|
|
|
* @throws {Error} On bad input buffer length if on safe mode. |
76
|
|
|
*/ |
77
|
|
|
export function unpackArrayTo( |
78
|
|
|
buffer: Uint8Array|number[], |
79
|
|
|
theType: object, |
80
|
|
|
output: number[]|ArrayBufferView|ArrayLike<number>, |
81
|
|
|
start?: number, |
82
|
|
|
end?: number, |
83
|
|
|
safe?: boolean): void; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Pack a number to a byte buffer. |
87
|
|
|
* @param {number} value The value. |
88
|
|
|
* @param {!{bits:number, fp: (boolean|undefined), signed: (boolean|undefined), be: (boolean|undefined)}} theType The type definition. |
89
|
|
|
* @param {!(Uint8Array|Array<number>)} buffer The byte buffer to write on. |
90
|
|
|
* @param {number} [index=0] The buffer index to write. |
91
|
|
|
* @param {boolean} [clamp=false] True to clamp ints on overflow. |
92
|
|
|
* @return {number} The next index to write. |
93
|
|
|
* @throws {Error} If the type definition is not valid. |
94
|
|
|
* @throws {RangeError} On overflow if clamp is set to false. |
95
|
|
|
* @throws {TypeError} If 'value' is not a number. |
96
|
|
|
* @throws {TypeError} If 'value' is not a int and type is int. |
97
|
|
|
*/ |
98
|
|
|
export function packTo( |
99
|
|
|
value: number, |
100
|
|
|
theType: object, |
101
|
|
|
buffer: Uint8Array|number[], |
102
|
|
|
index?: number, |
103
|
|
|
clamp?: boolean): number; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Pack a number as a array of bytes. |
107
|
|
|
* @param {number} value The number to pack. |
108
|
|
|
* @param {!{bits:number, fp: (boolean|undefined), signed: (boolean|undefined), be: (boolean|undefined)}} theType The type definition. |
109
|
|
|
* @param {boolean} [clamp=false] True to clamp ints on overflow. |
110
|
|
|
* @return {!Array<number>} The packed value. |
111
|
|
|
* @throws {Error} If the type definition is not valid. |
112
|
|
|
* @throws {RangeError} On overflow if clamp is set to false. |
113
|
|
|
* @throws {TypeError} If 'value' is not a number. |
114
|
|
|
* @throws {TypeError} If 'value' is not a int and type is int. |
115
|
|
|
*/ |
116
|
|
|
export function pack( |
117
|
|
|
value: number, |
118
|
|
|
theType: object, |
119
|
|
|
clamp?: boolean): number[]; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Unpack a number from a byte buffer. |
123
|
|
|
* @param {!(Uint8Array|Array<number>)} buffer The byte buffer. |
124
|
|
|
* @param {!{bits:number, fp: (boolean|undefined), signed: (boolean|undefined), be: (boolean|undefined)}} theType The type definition. |
125
|
|
|
* @param {number} [index=0] The buffer index to read. |
126
|
|
|
* @param {boolean} [safe=false] If set to false, extra bytes in the end of |
127
|
|
|
* the input array are ignored and input buffers with insufficient bytes will |
128
|
|
|
* write nothing to the output array. If safe is set to true the function |
129
|
|
|
* will throw a 'Bad buffer length' error on the aforementioned cases. |
130
|
|
|
* @return {number} |
131
|
|
|
* @throws {Error} If the type definition is not valid. |
132
|
|
|
* @throws {Error} On bad input buffer length if on safe mode. |
133
|
|
|
*/ |
134
|
|
|
export function unpack( |
135
|
|
|
buffer: Uint8Array|number[], |
136
|
|
|
theType: object, |
137
|
|
|
index?: number, |
138
|
|
|
safe?: boolean): number; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Pack a array of numbers as a array of bytes. |
142
|
|
|
* @param {!(Array<number>|TypedArray)} values The values to pack. |
143
|
|
|
* @param {!{bits:number, fp: (boolean|undefined), signed: (boolean|undefined), be: (boolean|undefined)}} theType The type definition. |
144
|
|
|
* @param {boolean} [clamp=false] True to clamp ints on overflow. |
145
|
|
|
* @return {!Array<number>} The packed values. |
146
|
|
|
* @throws {Error} If the type definition is not valid. |
147
|
|
|
* @throws {RangeError} On overflow if clamp is set to false. |
148
|
|
|
* @throws {TypeError} If 'values' is not a array of numbers. |
149
|
|
|
* @throws {TypeError} If 'values' is not a array of ints and type is int. |
150
|
|
|
*/ |
151
|
|
|
export function packArray( |
152
|
|
|
values: number[]|ArrayBufferView|ArrayLike<number>, |
153
|
|
|
theType: object, |
154
|
|
|
clamp?:boolean): Array<number>; |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Unpack a array of numbers from a byte buffer. |
158
|
|
|
* @param {!(Uint8Array|Array<number>)} buffer The byte buffer. |
159
|
|
|
* @param {!{bits:number, fp: (boolean|undefined), signed: (boolean|undefined), be: (boolean|undefined)}} theType The type definition. |
160
|
|
|
* @param {number} [start=0] The buffer index to start reading. |
161
|
|
|
* @param {number} [end=buffer.length] The buffer index to stop reading. |
162
|
|
|
* @param {boolean} [safe=false] If set to false, extra bytes in the end of |
163
|
|
|
* the input array are ignored and input buffers with insufficient bytes will |
164
|
|
|
* write nothing to the output array. If safe is set to true the function |
165
|
|
|
* will throw a 'Bad buffer length' error on the aforementioned cases. |
166
|
|
|
* @return {!Array<number>} |
167
|
|
|
* @throws {Error} If the type definition is not valid. |
168
|
|
|
* @throws {Error} On bad input buffer length if on safe mode. |
169
|
|
|
*/ |
170
|
|
|
export function unpackArray( |
171
|
|
|
buffer: Uint8Array|number[], |
172
|
|
|
theType: object, |
173
|
|
|
start?: number, |
174
|
|
|
end?: number, |
175
|
|
|
safe?: boolean): Array<number>; |
176
|
|
|
|